package com.sfteam.bmw.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.sfteam.bmw.vo.AjaxResult;

/**
 * 前端登录验证
 * 
 * @author knight322
 *
 */
public class TokenInterceptor implements Interceptor {

	static Log logger = Log.getLog(TokenInterceptor.class);

	private AjaxResult result = new AjaxResult();

	@Override
	public void intercept(Invocation inv) {
		System.out.println("我是TokenInterceptor");
		Controller ctrl = inv.getController();
		Cache userCache = Redis.use();
		String token = ctrl.getPara("token");
		if (StrKit.notBlank(token)) {
			// 取openid信息
			String openId = userCache.get(token + "_op");
			if (StrKit.notBlank(openId)) {
				inv.invoke();
			} else {
				result.addConfirmError("token失效，请重新登录！");
				ctrl.renderJson(result);
				return;
			}
		} else {
			result.addConfirmError("token失效，请重新登录！");
			ctrl.renderJson(result);
			return;
		}
	}

}
